//
//  SettingViewController.m
//  BDD_TWO
//
//  Created by XTW on 2023/5/29.
//

#import "SettingViewController.h"
#import "CustomAlertView.h"
#import "TabBarView.h"
#import <WFChatClient/WFCChatClient.h>
#import "AppService.h"
#import "PersonInfoViewController.h"
#import "WebShowViewController.h"
#import "AboutOurViewController.h"
#import "LogOffViewController.h"
#import "SDImageCache.h"

@interface SettingViewController ()
@property (weak, nonatomic) IBOutlet UIButton *logoutBtn;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self addBackBtn];
    [self initView];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.cacheLabel.text = [NSString stringWithFormat:@"%.2fM", [[SDImageCache sharedImageCache] totalDiskSize] / 1000.0 / 1000.0];
}

- (IBAction)personInfoPress {
    if ([UserManger hasUserLogged]) {
        PersonInfoViewController *viewCon = [[PersonInfoViewController alloc] initWithNibName:@"PersonInfoViewController" bundle:nil];
        [self.navigationController pushViewController:viewCon animated:YES];
    } else {
        [[TabBarView sharedTabBarView] actionLogin];
    }
}

- (IBAction)userAgreementPress {
    WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
    viewCon.urlStr = AgreementUrl;
    viewCon.titleStr = @"用户协议";
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)privacyAgreementPress {
    WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
    viewCon.urlStr = PrivacyUrl;
    viewCon.titleStr = @"隐私协议";
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)aboutPress {
    AboutOurViewController *viewCon = [[AboutOurViewController alloc] initWithNibName:@"AboutOurViewController" bundle:nil];
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)evaluatePress {
    NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@&pageNumber=0&sortOrdering=2&mt=8", APPLEID];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil];
}

- (IBAction)clearCachePress {
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        [self showHUDAlert:@"已清除"];
        self.cacheLabel.text = [NSString stringWithFormat:@"%.2fM", [[SDImageCache sharedImageCache] totalDiskSize] / 1000.0 / 1000.0];
    }];
}

- (IBAction)logoffPress {
    if (![UserManger hasUserLogged]) {
        [self showHUDAlert:@"未登录"];
        return;
    }
    LogOffViewController *viewCon = [[LogOffViewController alloc] initWithNibName:@"LogOffViewController" bundle:nil];
    [self.navigationController pushViewController:viewCon animated:YES];
}


- (IBAction)logoutPress {
    if (![UserManger hasUserLogged]) {
        [self showHUDAlert:@"未登录"];
        return;
    }
    CustomAlertView *alertView = [[CustomAlertView alloc] initItem:@"是否要退出当前账号？" withButtons:[NSArray arrayWithObjects:@"取消", @"退出", nil]];
    alertView.SureHandle = ^{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedName"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedToken"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"savedUserId"];
        [[AppService sharedAppService] clearAppServiceAuthInfos];
        [[NSUserDefaults standardUserDefaults] synchronize];
        //退出后就不需要推送了，第一个参数为YES
        //如果希望再次登录时能够保留历史记录，第二个参数为NO。如果需要清除掉本地历史记录第二个参数用YES
        [[WFCCNetworkService sharedInstance] disconnect:YES clearSession:NO];
        
        [UserManger logoutCurrentUser:nil];
        [self.navigationController popToRootViewControllerAnimated:NO];
        [[TabBarView sharedTabBarView] actionLogin];
    };
    [alertView show];
}

- (void)initView {
    [self.scrollView setFrame:CGRectMake(0, IS_iPhoneX_Top + NavBarHeight, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Top - NavBarHeight - IS_iPhoneX_Bottom - 44 - 10)];
    [self.scrollView setContentSize:CGSizeMake(0, 535)];
    [self.logoutBtn setFrame:CGRectMake(15, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Bottom - 10 - 44, SCREEN_WIDTH_DEVICE - 30, 44)];
    [self.logoutBtn setBackgroundImage:getColorToImage(TextGrayColor) forState:UIControlStateNormal];

}
@end
